package com.gitee.sop.layui.adminserver.module.base.view;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author: huwei
 * @date: 2019/9/17 19:25
 * @version: 1.3.0
 */
public abstract class BaseViewController {

    @RequestMapping(value = {"/{path}"})
    public ModelAndView forward(@PathVariable("path") String path, ModelAndView modelAndView, Long id, String key, HttpServletRequest request, HttpServletResponse response) {
        modelAndView.setViewName(prefix().concat("/" + path + ".html"));
        modelAndView.addObject("id", id);
        modelAndView.addObject("key", key);
        return modelAndView;
    }


    @RequestMapping(value = {"" ,"/" ,"/start" ,"/index" ,"/index.html" ,"/index.htm" ,"/home" ,"/home.html"})
    public ModelAndView index(ModelAndView modelAndView , HttpServletRequest request , HttpServletResponse response){
        modelAndView.setViewName(prefix().concat("/index.html"));
        return modelAndView;
    }

    @RequestMapping(value = {"/add"})
    public ModelAndView add(ModelAndView modelAndView, Long id , String key , HttpServletRequest request , HttpServletResponse response){
        modelAndView.setViewName(prefix().concat("/add.html"));
        modelAndView.addObject("id" ,id);
        modelAndView.addObject("key" ,key);
        return modelAndView;
    }

    @RequestMapping(value = {"/edit"})
    public ModelAndView edit(ModelAndView modelAndView , Long id , String key , HttpServletRequest request , HttpServletResponse response){
        modelAndView.setViewName(prefix().concat("/edit.html"));
        modelAndView.addObject("id" ,id);
        modelAndView.addObject("key" ,key);
        return modelAndView;
    }

    @RequestMapping(value = {"/detail"})
    public ModelAndView detail(ModelAndView modelAndView , Long id , String key , HttpServletRequest request , HttpServletResponse response){
        modelAndView.setViewName(prefix().concat("/detail.html"));
        modelAndView.addObject("id" ,id);
        modelAndView.addObject("key" ,key);
        return modelAndView;
    }

    /**
     * 返回页面的前缀路径地址，格式如下：
     * module/test/
     * @return
     */
    protected abstract String prefix();

}
